﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using BaoJianSoft.Common;
using BaoJianSoft.RapidWeb;
using BaoJianSoft.RapidWeb.Controls;
using BaoJianSoft.RapidWeb.DynamicPages;
using BaoJianSoft.RapidWeb.DynamicPages.Configurations;

namespace BaoJianSoft.ExtensionModel.Web.DynamicPage
{
	/// <summary>
	/// Extension field metadata management dynamic page handler.
	/// </summary>
	public class ExtensionFieldMetadataManagement : BaoJianSoft.RapidWeb.DynamicPages.DynamicPage
	{
		private static object syncObject = new object();
		private static IApplicationContext applicationContext = SpringContext.Current.GetObject<IApplicationContext>();
		private static IMetadataApi metadataApi = SpringContext.Current.GetObject<IMetadataApi>();

		/// <summary>
		/// Execute query for results binding to dynamic page grid.
		/// </summary>
		/// <param name="parameter">Query parameter.</param>
		/// <returns>Returns query results.</returns>
		public override QueryResults Query(QueryParameter parameter)
		{
			IEnumerable<IFieldMetadata> fieldMetadataEnumerable = metadataApi.GetFields(QueryStringUtility.MetadataDataTypeId);
			if (parameter.SortExpression != null)
				fieldMetadataEnumerable = fieldMetadataEnumerable.AsQueryable().OrderBy(parameter.SortExpression.Compile());

			return new QueryResults(fieldMetadataEnumerable.Count(), fieldMetadataEnumerable);
		}

		/// <summary>
		/// Delete field metadata by id.
		/// </summary>
		/// <param name="entityId"></param>
		public override void Delete(string entityId)
		{
			string fieldName = entityId;
			metadataApi.DeleteField(QueryStringUtility.MetadataDataTypeId, fieldName);
		}

		/// <summary>
		/// Validate query string "MetadataDataTypeId".
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public override void OnInit(object sender, EventArgs e)
		{
			IObjectMetadata metadata = metadataApi.GetType(QueryStringUtility.MetadataDataTypeId);
			if (metadata == null)
				WebUtility.RedirectToPage(WebUtility.PageNotFoundUrl);
		}

		/// <summary>
		/// Cannot modify and delete the inherited fields from parents.
		/// </summary>
		/// <param name="e"></param>
		public override void OnGridRowControlsBind(GridRowControlBindEventArgs e)
		{
			bool inherited = (bool)DataBinder.Eval(e.DataItem, "Inherited");
			e.ShowDeleteButton = !inherited;
			e.ShowEditButton = !inherited;
		} 

		/// <summary>
		/// Validate query string "MetadataDataTypeId" and setup metadata data type name to IAuthenticationContext.TempVariables.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public override void SetupContextTempVariables(object sender, SetupApplicationContextVariablesEventArgs e)
		{
			IObjectMetadata metadata = metadataApi.GetType(QueryStringUtility.MetadataDataTypeId);
			if (metadata == null)
				WebUtility.RedirectToPage(WebUtility.PageNotFoundUrl);

			applicationContext.TempVariables["ExtensionDataTypeName"] = metadata.Name;
		}
	}
}
